package com.aduerr.v1shouyin.mvp.activity;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.KaQuanXiangQingEntity;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.aduerr.v1shouyin.view.BandOrUnbandGuidResultDialog;
import com.aduerr.v1shouyin.view.HexiaoInputMoneyDialog;

public class KaQuanDetailActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_tiaojian)
    TextView tvTiaojian;
    @BindView(R.id.tv_confirm_hexiao)
    TextView tvConfirmHexiao;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_user_konw)
    TextView tvUserKonw;
    @BindView(R.id.ll_all_bg)
    LinearLayout bg;//整体背景
    RelativeLayout rl_title;
    private HexiaoInputMoneyDialog hexiaoInputMoneyDialog;
    private KaQuanXiangQingEntity.DataBean.ListBean date;
    private String erWeiMaResult;
    private String minmoney;
    private BandOrUnbandGuidResultDialog kaQuanHeXiaoChengGongDialog;
    private String editordermony;
    /*
     * enable   true  可以核销         背景灰色#cbcaca
     *
     *
     * */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ka_quan_detail);
        ButterKnife.bind(this);
        rl_title = (RelativeLayout) findViewById(R.id.rl_title);
        tvTitle.setText("核销");
        initdate();
    }

    private void initdate() {
        date = (KaQuanXiangQingEntity.DataBean.ListBean) getIntent().getSerializableExtra("date");
        erWeiMaResult = getIntent().getStringExtra("erWeiMaResult");

        if(date != null) {
//            KaQuanXiangQingEntity.DataBean.ListBean listBean = date.getData().get(0)
//            .getList().get(0);
            int isuse = date.getIsuse();
            minmoney = date.getMinmoney();
            if(isuse == 0) {//没有核销
                tvConfirmHexiao.setEnabled(true);
                tvTitleName.setText(date.getName());
                tvName.setText(date.getDescription());
                tvTime.setText(date.getBegintime() + "至" + date.getEndtime());
                tvTiaojian.setText("订单满" + date.getMinmoney() + "可用");
                tvUserKonw.setText(date.getUsershuoming());
            } else if(isuse == 1) {
                imgBack.setImageResource(R.drawable.img_back_white);
                bg.setBackgroundColor(getResources().getColor(R.color.kaquanhexiaobggray));
                rl_title.setBackgroundColor(getResources().getColor(R.color.kaquanhexiaobggray));
                tvConfirmHexiao.setEnabled(false);
                tvConfirmHexiao.setText("已核销");
                tvTitleName.setText(date.getName());
                tvName.setText(date.getDescription());
                tvTime.setText(date.getBegintime() + date.getEndtime());
                tvTiaojian.setText("订单满" + date.getMinmoney() + "可用");
                tvUserKonw.setText(date.getUsershuoming());
            }
        }
    }

    @OnClick({R.id.img_back, R.id.tv_confirm_hexiao})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_confirm_hexiao:
                //// TODO: 2017/12/13 0013  核销按钮
                tvConfirmHexiao.setEnabled(true);
                //第一个是取消按钮,第二个是确认按钮
                hexiaoInputMoneyDialog =
                        new HexiaoInputMoneyDialog(KaQuanDetailActivity.this,
                                R.style.loading_dialog, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        hexiaoInputMoneyDialog.dismiss();
                        editordermony = hexiaoInputMoneyDialog.getEditContent();
                        if(erWeiMaResult == null | TextUtils.isEmpty(editordermony)) {
                            ToastUtils.showToast(KaQuanDetailActivity.this, "请输入核销的金额");
                            return;
                        }
                        if(erWeiMaResult != null && !TextUtils.isEmpty(editordermony)) {
                            responseNetForHexiao(erWeiMaResult, editordermony);
                        }
                    }
                });
                hexiaoInputMoneyDialog.show();
//                if (erWeiMaResult != null && minmoney != null) {
//                    responseNetForHexiao(erWeiMaResult, minmoney);
//                }
                break;
        }
    }

    //请求网络为了 核销该张优惠券
    private void responseNetForHexiao(String scanTicketNo, String minmoney) {
       /* HashMap<String, String> addParams = new HashMap<>();
        addParams.put("scanTicketNo", scanTicketNo);//86008032893055438199  test
        addParams.put("paymoney", minmoney);
        APIRetrofit.getAPIService().getKaQuanHeXiao(RXRequest.getXingPosParams
        (addParams, this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BlockingBaseObserver<KaQuanXiangQingDetailEntity>() {
                               @Override
                               public void onNext(KaQuanXiangQingDetailEntity
                               kaQuanXiangQingDetailEntity) {
                                   //                   if (Tools.isAvailable
                                   (kaQuanXiangQingDetailEntity))return;
                                   int success = kaQuanXiangQingDetailEntity.getSuccess();
                                   if (success == 1) {
//                            //右上角的小红叉  之前版本低的卡券核销
//                            kaQuanHeXiaoChengGongDialog = new
BandOrUnbandGuidResultDialog(KaQuanDetailActivity.this, R.style.loading_dialog, new
View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    //右上角的小红叉
//                                    kaQuanHeXiaoChengGongDialog.hide();
//                                    finish();
//                                }
//                            }, 106);
//                            kaQuanHeXiaoChengGongDialog.show();
//                            kaQuanHeXiaoChengGongDialog.setMoney(kaQuanHeXiaoEntity
.getTouse());
//                            new Handler().postDelayed(new Runnable() {
//                                @Override
//                                public void run() {
//                                    finish();
//                                }
//                            }, 3200);
//=================================================================================================
                                       Intent intent = new Intent(KaQuanDetailActivity
                                       .this, CardUseSuccessActivity.class);
                                       intent.putExtra("memberNo", scanTicketNo);
                                       intent.putExtra("member",
                                       kaQuanXiangQingDetailEntity.getData()
                                       .getMembername());
                                       intent.putExtra("uTime",
                                       kaQuanXiangQingDetailEntity.getData()
                                       .getTousedate());
                                       intent.putExtra("title",
                                       kaQuanXiangQingDetailEntity.getData().getTitle());
//                            intent.putExtra("biztype", bizType);
                                       intent.putExtra("touse",
                                       kaQuanXiangQingDetailEntity.getData().getTouse());
                                       String needpay = kaQuanXiangQingDetailEntity
                                       .getData().getNeedpay();
                                       double Dneedpay = Double.parseDouble(needpay);
                                       if (Dneedpay > 0) {
                                           //之前版本!!
                                           intent.putExtra("paymoney", needpay);
                                       }
                                       startActivity(intent);
                                       finish();


                                   } else if (success == -100 | success == -200) {
                                       //// TODO: 2017/12/9 0009 绑定guid  界面
                                       SharedPreferencesUtils.remove
                                       (KaQuanDetailActivity.this, "autologin");
                                       SharedPreferencesUtils.remove
                                       (KaQuanDetailActivity.this, "remberpassword");
                                       startActivity(new Intent(KaQuanDetailActivity
                                       .this, BangGuidActivity.class));
                                       finish();
                                   } else if (success == -300 || success == -2 ||
                                   success == -3 || success == -6) {
                                       //// TODO: 2017/12/9 0009 去登录的界面
                                       com.blankj.utilcode.util.ToastUtils
                                       .showShortSafe(kaQuanXiangQingDetailEntity
                                       .getErrMsg());
                                       SharedPreferencesUtils.remove
                                       (KaQuanDetailActivity.this, "autologin");
                                       SharedPreferencesUtils.remove
                                       (KaQuanDetailActivity.this, "remberpassword");
                                       startActivity(new Intent(KaQuanDetailActivity
                                       .this, LoginActivity.class));
                                       finish();
                                   } else {
                                       String errMsg = kaQuanXiangQingDetailEntity
                                       .getErrMsg();
                                       ToastUtils.showToast(KaQuanDetailActivity.this,
                                       errMsg + "");
                                   }
                               }

                               @Override
                               public void onError(Throwable e) {
                                   if (e instanceof SocketTimeoutException) {
                                       Toast.makeText(KaQuanDetailActivity.this,
                                       "网络连接超时", Toast.LENGTH_SHORT).show();
                                   } else if (e instanceof ConnectException) {
                                       Toast.makeText(KaQuanDetailActivity.this,
                                       "连接失败", Toast.LENGTH_SHORT).show();
                                   } else {
                                       Log.e("onError:", "error:" + e.getMessage());//R
                                       .string.checknet +
                                       Toast.makeText(KaQuanDetailActivity.this,
                                       "检查网络状态", Toast.LENGTH_SHORT).show();
                                   }
                               }
                           });
*/
    }

}
